package com.qcby.mycommunity_003.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qcby.mycommunity_003.annotation.LogAnnotation;
import com.qcby.mycommunity_003.entity.Community;
import com.qcby.mycommunity_003.entity.ManualRecord;
import com.qcby.mycommunity_003.entity.User;
import com.qcby.mycommunity_003.mapper.ManualRecordMapper;
import com.qcby.mycommunity_003.service.CommunityService;
import com.qcby.mycommunity_003.service.ManualRecordService;
import com.qcby.mycommunity_003.util.Result;
import com.qcby.mycommunity_003.vo.ManualListVo;
import com.qcby.mycommunity_003.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-03-28
 */
@RestController
@RequestMapping("/sys/manual")
public class ManualRecordController {
    @Autowired
    private ManualRecordService manualRecordService;
    @Autowired
    private ManualRecordMapper manualRecordMapper;
    @Autowired
    private CommunityService communityService;

    /**
     * 展示列表
     * @param manualListVo
     * @return
     */
    @GetMapping("/list")
    public Result ManualList(ManualListVo manualListVo){
        List<Community> communityList = communityService.list();
        PageVo pageVo=manualRecordService.ManualPage(manualListVo);
        HashMap<String,Object> map01=new HashMap<>();
        //下拉框选择小区
        map01.put("communityList",communityList);
        //封装分页
        map01.put("pageList",pageVo);
        return Result.ok().put("data",map01);
    }

    /**
     * 添加
     * @param manualRecord
     * @return
     */
    @LogAnnotation("访客登记")
    @PostMapping("/add")
    public Result Add(@RequestBody ManualRecord manualRecord, HttpSession session){
        User user = (User) session.getAttribute("user");
        manualRecord.setUserName(user.getUsername());
        manualRecordService.save(manualRecord);
        return Result.ok();
    }

    /**
     * 回显
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public Result infoManual(@PathVariable Integer id){
        if(id==0){
            HashMap <String,Object> map=new HashMap<String,Object> ();
            List<Community> communityList = communityService.list();
            map.put("communityList",communityList);
            return Result.ok().put("data",map);
        }else {
            HashMap <String,Object> map=new HashMap<String,Object> ();
            List<Community> communityList = communityService.list();
            map.put("communityList",communityList);
            ManualRecord manualInfo = manualRecordService.getById(id);
            map.put("manualInfo",manualInfo);
            return Result.ok().put("data",map);
        }
    }

    /**
     * 修改
     * @param manualRecord
     * @return
     */
    @LogAnnotation("修改访客信息")
    @PutMapping("/edit")
       public Result edit(@RequestBody ManualRecord manualRecord){
        QueryWrapper<ManualRecord> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("manual_record_id",manualRecord.getManualRecordId());
        manualRecordService.update(manualRecord,queryWrapper);
        return Result.ok();
       }
       @LogAnnotation("删除拜访记录")
       @DeleteMapping("/del")
       public Result del(@RequestBody List<Integer> list){
        manualRecordMapper.deleteBatchIds(list);
        return Result.ok();
       }
}

